﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CWCS.Entity.Convery;
using C1.Win.C1FlexGrid;
using CWCS.Entity.Robot;

namespace CWCS.UI
{
    public partial class FrmCheckInfo :Form
    {
        private delegate void DataGridViewCallBack(C1FlexGrid grd, List<CheckInfo> lst);
        public FrmCheckInfo()
        {
            InitializeComponent();
         
        }

      private  string stationno = string.Empty;
       private string traycode = string.Empty;


        private void GenerateDataTable(C1FlexGrid grd, List<CheckInfo> lst)
        {
            if (grd.InvokeRequired)
            {
                DataGridViewCallBack grdcall = new DataGridViewCallBack(GenerateDataTable);
                this.Invoke(grdcall, new object[] { grd, lst });
            }
            else
            {
                try
                {
                    DataTable dtData = new DataTable();
                    dtData = MakeDateTable("dtCheckInfo", "SerialNum", "序号", "CheckItem", "检查项", "DataInfo", "分析数据", "CheckResult", "检查结论");
                   foreach (var item in lst)
                   {
                       DataRow dr = dtData.NewRow();
                       dr["SerialNum"] = item.SerialNum.ToString().Trim();
                       dr["CheckItem"] = item.CheckItem.ToString().Trim();
                       dr["DataInfo"] = item.DataInfo.ToString().Trim();
                       dr["CheckResult"] = item.CheckResult.ToString().Trim();
                       dtData.Rows.Add(dr);
                   }
                   grd.Rows.Count = grd.Rows.Fixed;
                   if (dtData.Rows.Count > 0)
                       SetRowData(grd,dtData);
                }
                catch
                {
                    throw;
                }
            }
        }

        private void SetRowData(C1FlexGrid grd, DataTable dt)
        {
            // int i = 0;
            try
            {
                foreach (DataRow dataRow in dt.Rows)
                {
                    Row row = grd.Rows.Add();
                    row.UserData = dataRow;

                    foreach (DataColumn dataColumn in dt.Columns)
                    {
                        if (grd.Cols.Contains(dataColumn.Caption.Trim()))
                        {
                            row[dataColumn.Caption] = dataRow[dataColumn.Caption];
                        }
                        else
                        {
                            if (grd.Cols.Contains(dataColumn.ColumnName))
                                row[dataColumn.ColumnName] = dataRow[dataColumn.ColumnName];
                        }
                    }
                }
            }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
            {
               // throw ex;
            }
        }

        private DataTable MakeDateTable(string tableName, string colName1, string colCaption1, string colName2, string colCaption2, string colName3, string colCaption3, string colName4, string colCaption4)
        {
            DataTable dataTable = new DataTable();
            dataTable.TableName = tableName;

            DataColumn taskColumn1 = new DataColumn();
            taskColumn1.DataType = System.Type.GetType("System.String");
            taskColumn1.ColumnName = colName1;
            taskColumn1.Caption = colCaption1;
            taskColumn1.MaxLength = 5;
            dataTable.Columns.Add(taskColumn1);

            DataColumn taskColumn2 = new DataColumn();
            taskColumn2.DataType = System.Type.GetType("System.String");
            taskColumn2.ColumnName = colName2;
            taskColumn2.Caption = colCaption2;
            taskColumn2.MaxLength = 300;
            dataTable.Columns.Add(taskColumn2);

            DataColumn taskColumn3 = new DataColumn();
            taskColumn3.DataType = System.Type.GetType("System.String");
            taskColumn3.ColumnName = colName3;
            taskColumn3.Caption = colCaption3;
            taskColumn3.MaxLength = 50;
            dataTable.Columns.Add(taskColumn3);


            DataColumn taskColumn4 = new DataColumn();
            taskColumn4.DataType = System.Type.GetType("System.String");
            taskColumn4.ColumnName = colName4 ;
            taskColumn4.Caption = colCaption4;
            taskColumn4.MaxLength = 50;
            dataTable.Columns.Add(taskColumn4);
            return dataTable;
        }

        public void ShowData(string station, string barcode, List<CheckInfo> lst)
        {
            if (lst.Count > 0)
            {
                this.Text = string.Format("站台：{0}，托盘：{1}，入库条件检查！", station, barcode);// +barcode + "，绑盘信息";
                GenerateDataTable(c1FlexGrid1, lst);
            }
            else
            {
                MessageBox.Show("检查信息为空");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

    }
}
